<?php

declare(strict_types=1);

/** 自动化测试用例
 * User：Abell
 * Date：2020/5/16
 */

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Controller;
//log
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\FirePHPHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

/**
 * @Controller()
 */
class LogController extends AbstractController
{


    /** 写入本地log日志 ok
     * @param bool force
     * @return array
     * Abell 2020/5/20
     * @RequestMapping(path="/abell/log/test", methods="get,post")
     */
    public function test(){
        $log = new Logger('log');
        $stream = new StreamHandler('test.log',Logger::WARNING);
        $fire = new FirePHPHandler();
        $dateFormat = "Y n j, g:i a";
// 定义日志格式为 "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $output = "%datetime%||%channel||%level_name%||%message%||%context%||%extra%\n";
// 根据 时间格式 和 日志格式，创建一个 Formatter
        $formatter = new LineFormatter($output, $dateFormat);
// 将 Formatter 设置到 Handler 里面
        $stream->setFormatter($formatter);
// 将 Handler 推入到 Channel 的 Handler 队列内
        $log->pushHandler($stream);
        $log->pushHandler($fire);

        $log->warning('hello warn');
        $log->error('hello error',['username'=>'abell']);

        $log->pushProcessor(function ($record){
           $record['extra']['dummy'] = 'hello';
           return $record;
        });
        $log->pushProcessor(new \Monolog\Processor\MemoryPeakUsageProcessor());
        $log->alert('abell');
    }

    /** 写入本地log日志 ok
     * @param bool force
     * @return array
     * Abell 2020/5/20
     * @RequestMapping(path="/abell/log/hello", methods="get,post")
     */
    public function hello(RequestInterface $request){
        // 创建一个 Channel，参数 log 即为 Channel 的名字
        $log = new Logger('log');
// 创建两个 Handler，对应变量 $stream 和 $fire
        $stream = new StreamHandler('test.log', Logger::WARNING);
        $fire = new FirePHPHandler();
// 定义时间格式为 "Y-m-d H:i:s"
        $dateFormat = "Y n j, g:i a";
// 定义日志格式为 "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
        $output = "%datetime%||%channel||%level_name%||%message%||%context%||%extra%\n";
// 根据 时间格式 和 日志格式，创建一个 Formatter
        $formatter = new LineFormatter($output, $dateFormat);
// 将 Formatter 设置到 Handler 里面
        $stream->setFormatter($formatter);
// 将 Handler 推入到 Channel 的 Handler 队列内
        $log->pushHandler($stream);
        $log->pushHandler($fire);
// clone new log channel
        $log2 = $log->withName('log2');
// add records to the log
        $log->warning('Foo');
// add extra data to record
// 1. log context
        $log->error('a new user', ['username' => 'daydaygo']);
// 2. processor
        $log->pushProcessor(function ($record) {
            $record['extra']['dummy'] = 'hello';
            return $record;
        });
        $log->pushProcessor(new \Monolog\Processor\MemoryPeakUsageProcessor());
        $log->alert('czl');
    }
}